package com.demo2do.fms.admin.web.controller.supplier.sub.info;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.export.supplier.DriverSheet;
import com.demo2do.fms.admin.entity.search.supplier.DriverSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 * 分属供方司机信息查询
 * 业务逻辑：
 *    查询分属供方司机信息
 *        - 总部：可看所有司机信息
 *        - 分区：可看所有司机信息
 *        - 一级供方：可看所有属于该一级供方的二级供方的司机信息 （如果要看一级供方自己的司机信息，请至司机信息查询）
 *        - 二级供方： 无可看
 * Created by zhonghui on 2017/6/21.
 */
@Controller
@RequestMapping("/sub-drivers")
@MenuSetting("menu-supplier")
public class SubDriverInfoController {
    
    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private DriverService driverService;

    /**
     * list 司机信息
     * 
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-sub-driver-management")
    public ModelAndView list(@Secure Login login, HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/driver/sub-driver-info-list");
        
        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        
        int totals = driverService.count(supplierIds);
        if(totals > 0) {
            
            List<Driver> drivers = driverService.list(supplierIds, page.with(totals));
            
            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }

    /**
     * search 司机信息查询
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure Login login) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/driver/sub-driver-info-search");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login, Type.MOTOR);
        modelAndView.addObject("suppliers", suppliers);

        return modelAndView;
        
    }

    /**
     * 
     * @param login
     * @param driverSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-sub-driver-management")
    public ModelAndView onSearch(@Secure Login login, DriverSearch driverSearch, 
                                 HttpServletRequest request, Page page) {


    	ModelAndView modelAndView = new ModelAndView("supplier/sub/info/driver/sub-driver-info-list");

        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        driverSearch.addSupplierId(supplierIds);
        
        int totals = driverService.count(driverSearch);
        if (totals > 0) {
            
            List<Driver> drivers = driverService.list(driverSearch, page.with(totals));
            
            modelAndView.addObject("drivers", drivers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }
    
    /**
     * 导出所属司机
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/driver/sub-driver-info-export");
        
        List<Supplier> suppliers = supplierService.listSubSupplier(login, Type.MOTOR);
        modelAndView.addObject("suppliers", suppliers);
        
        return modelAndView;
        
    }
    
    /**
     * 导出所属司机
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @MenuSetting("submenu-sub-driver-management")
    public ModelAndView onExport(DriverSearch search) {
    	
    	ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("supplier/driver-export.xlsx"));
    	
    	List<DriverSheet> driverSheets = driverService.export(search);
    	modelAndView.addObject("drivers", driverSheets);
    	
    	return modelAndView;
    	
    }

}
